//
//  OMOrderRefundDetailViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/16.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderRefundDetailViewController.h"
#import "OMOrderRefundDetail.h"
#import "OMOrder.h"
#import "OMOrderRefundPriceTableCell.h"
#import "OMOrderRefundDetailTableCell.h"
#import "OMOrderRefundStatusTableCell.h"


#import "NSString+KTExt.h"
@interface OMOrderRefundDetailViewController ()
@property (nonatomic, strong) OMOrderRefundDetail *detail;
@property (nonatomic, strong) OMOrderRefundPriceTableCell *priceCell;
@property (nonatomic, strong) OMOrderRefundDetailTableCell *detailCell;
@property (nonatomic, strong) OMOrderRefundStatusTableCell *statusCell;
@end

@implementation OMOrderRefundDetailViewController {
    NSArray * _cellArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self startAnimating:YES];
    [self getOrderRefundDetail];
}

- (void)setupUI
{
    self.title = @"退款详情";
    self.tableView.sectionFooterHeight = DBL_EPSILON;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _priceCell = [OMOrderRefundPriceTableCell new];
    _detailCell = [OMOrderRefundDetailTableCell new];
    _statusCell = [OMOrderRefundStatusTableCell new];
    _cellArray = @[_priceCell,_detailCell];

    [self setupFooter];


}

- (void)reloadDataFromNetWork
{
    [self getOrderRefundDetail];
}

- (void)getOrderRefundDetail
{
    [KTHttpUtils
     GET:@"member/refunds/view.jhtml"
     parameters:@{@"id":self.refundId}
     needCache:NO
     needToken:YES
     progress:nil
     completion:^(id responseObject, BOOL isSuccess, NSString *message) {
         NSLog(@"%@",responseObject);
         if (isSuccess) {
             self.detail = [OMOrderRefundDetail mj_objectWithKeyValues:responseObject[OMDatumKey]];
             [self startAnimating:NO];
         }else {
             [self showNetWorkError];
         }
    }];
}

- (void)setDetail:(OMOrderRefundDetail *)detail
{
    _detail = detail;
    [self.priceCell configureCellWithObj:detail];
    [self.detailCell configureCellWithObj:detail];
}

- (void)setupFooter
{
    UIView *bottomView = [UIView new];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@(SM_SIZE(0)));
        make.height.equalTo(@(SM_SIZE(140)));
    }];

    UIView *midView = [UIView new];
    midView.userInteractionEnabled = true;
    [bottomView addSubview:midView];
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView.mas_centerX);
        make.bottom.equalTo(bottomView.mas_centerY).offset(SM_SIZE(-5));
    }];

    UIImageView *phoneIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_order_service_phone"]];
    [midView addSubview:phoneIcon];
    [phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SM_SIZE(25), SM_SIZE(25)));
    }];

    UILabel *phoneLabel = [UILabel new];
    phoneLabel.font = SM_NFont(25);
    phoneLabel.textColor = HEXCOLOR(@"#6598D5");
    phoneLabel.text = @"400-930-2220";
    [midView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneIcon.mas_right).offset(SM_SIZE(10));
        make.right.equalTo(@0);
        make.centerY.equalTo(phoneIcon.mas_centerY);
    }];

    UILabel *descLabel = [UILabel new];
    descLabel.font = SM_CFont(25);
    descLabel.textColor = SM_darkGrayColor;
    descLabel.text = @"如有疑问,请拨打客服电话";
    [bottomView addSubview:descLabel];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView.mas_centerX);
        make.top.equalTo(bottomView.mas_centerY).offset(SM_SIZE(5));
    }];

    @weakify(phoneLabel);
    UITapGestureRecognizer *tapPhone = [[UITapGestureRecognizer alloc]init];
    [midView addGestureRecognizer:tapPhone];
    [[tapPhone rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(phoneLabel);
        [phoneLabel.text makeCall];
    }];
}

#pragma mark - tableview 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!_cellArray) {
        return 0;
    }
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _cellArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return _cellArray[indexPath.row];
    }else if (indexPath.section == 1) {
        return _statusCell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return [[_cellArray[indexPath.row] class] cellHeight];
    }else if (indexPath.section == 1) {
        return [OMOrderRefundStatusTableCell cellHeight];
    }
    return 0;
}
@end
